<?php
namespace Seller\Model;
use Think\Model;

/**
 * 商家
 */
class UserModel extends Model{

    /**
     * 更新用户信息
     */
    public function up_user($feild,$where,$param){
        $user = M('user');
        $map[$feild] = $where;
        $data = $user->where($map)->save($param);
        Addlog();
        return $data;
    }

    /**
     * 添加用户
     */
    public function add_user($param){
        $user = M('user');
        $data = $user->data($param)->add();
        Addlog();
        return $data;
    }

    /**
     * 读取用户数据
     */
    public function user_find($feild,$where){
        $user = M('user');
        $map[$feild] = $where;
        $data = $user->where($map)->find();
        Addlog();
        return $data;
    }

    /**
     * 获取收藏商家的用户
     */
    public function getCollectList($go_company = 0)
    {
        $collect= M('collect');
        $result = array();
        $count	= $collect->join('cs_user ON cs_collect.col_user = cs_user.us_id', 'LEFT')->where('cs_collect.col_item = '.$go_company.' AND cs_collect.col_type = 1')->field('COUNT(*) AS count')->find();
        $result	= $collect->join('cs_user ON cs_collect.col_user = cs_user.us_id', 'LEFT')->where('cs_collect.col_item = '.$go_company.' AND cs_collect.col_type = 1')->order('cs_collect.col_time DESC')->field('cs_user.us_uid, cs_user.us_mobile, cs_user.us_img')->select();
        $result[] = $count;
        return $result;
    }

    /**
     * 全局方法
     */
    /*******************************查询数据**************************************/
    /**
     * 查询单数据
     * $table string  表名
     * $feild string  where字段名
     * $where string  where的值
     */
    public function data_find($table,$feild,$where){
        $data = M($table);
        $map[$feild] = $where;
        $result = $data->where($map)->find();
        Addlog();
        return $result;
    }

    /**
     * 查询所有数据
     * $table string  表名
     * $feild string  where字段名
     * $where string  where的值
     */
    public function data_sel($table,$feild,$where,$order,$limit){
        $data = M($table);
        if(!is_null($feild)){
            $map[$feild] = $where;
        }
        $result = $data->field($feild)->where($map)->order($order)->limit($limit)->select();
        Addlog();
        return $result;
    }

    /**
     * 获取单个字段的值
     * $table string  表名
     * $feild string  where字段名
     * $where string  where的值
     * getfield string  字段名
     */
    public function data_getfeild($table,$feild,$where,$getfield){
        $data = M($table);
        $map[$feild] = $where;
        $result = $data->where($map)->getField($getfield);
        Addlog();
        return $result;
    }

    /**
     * 利用where数组查询数据
     * $table string  表名
     * $where array  where数组
     */
    public function data_find_where_array($table,$where){
        $data = M($table);
        $result = $data->where($where)->find();
        Addlog();
        return $result;
    }

    /**
     * 利用where数组获取单个字段的值
     * $table string  表名
     * $where array  where数组
     * $getfield string  字段名
     */
    public function data_getfeild_where_array($table,$where,$getfield){
        $data = M($table);
        $result = $data->where($where)->getField($getfield);
        Addlog();
        return $result;
    }

    /**
     * 利用where数组查询所有数据
     * $table string  表名
     * $where array  where数组
     */
    public function data_sel_where_array($table,$where,$order,$limit){
        $data = M($table);
        if(!is_null($where)){
            $result = $data->where($where)->order($order)->limit($limit)->select();
        }else{
            $result = $data->where($map)->order($order)->limit($limit)->select();
        }
        Addlog();
        return $result;
    }

    /**
     * 多表查询数据
     */
    public function datasel_multi_table_string($table,$where,$feild,$order,$limit,$type){
        switch ($type) {
            case '1':
                $result = M()->table($table)->where($where)->field($feild)->order($order)->limit($limit)->select();
                break;
            case '10':
                $result = M()->table($table)->where($where)->field($feild)->count();
                break;
            case '20':
                $result = M()->table($table)->where($where)->field($feild)->find();
                break;
            default:
                $result = M()->table($table)->where($where)->field($feild)->order($order)->limit($limit)->select();
                break;
        }
        Addlog();
        return $result;
    }



    /*******************************更新数据**************************************/

    /**
     * 更新数据
     * $table string  表名
     * $feild string  where字段名
     * $where string  where的值
     */
    public function up_data($table,$feild,$where,$param){
        $data = M($table);
        $map[$feild] = $where;
        $result = $data->where($map)->save($param);
        Addlog();
        return $result;
    }

    /**
     * 更新数据
     * $table string  表名
     * $where array  where数组
     * $param array  更新数组
     */
    public function up_data_where_array($table,$where,$param){
        $data = M($table);
        $result = $data->where($where)->save($param);
        Addlog();
        return $result;
    }

    /**
     * 更新数据
     * $table string  表名
     * $param array  更新数组
     */
    public function up_data_array($table,$param){
        $data = M($table);
        $result = $data->save($param);
        Addlog();
        return $result;
    }

    /**
     * 更新字段数增减
     * $table string  表名
     * $feild string  where字段名
     * $where string  where的值
     * $param array   需要更新数据
     * $type string   类型 setinc增加   setdec减少
     */
    public function data_setInc_setDec($table,$where,$field,$nums,$type){
        $data = M($table);
        if($type == "setinc"){
            $result = $data->where($where)->setInc($field,$nums);
        }
        if($type == "setdec"){
            $result = $data->where($where)->setDec($field,$nums);
        }
        return $result;
    }


    /*******************************删除数据**************************************/

    /**
     * 删除数据
     * $table string  表名
     * $feild string  where字段名
     * $where string  where的值
     */
    public function data_del($table,$feild,$where){
        $data = M($table);
        $map[$feild] = $where;
        $result = $data->where($map)->delete();
        Addlog();
        return $result;
    }

    /**
     * 利用where数组删除数据
     * 多个where条件
     * $table string  表名
     * $where array  数组
     */
    public function data_del_where_array($table,$where){
        $data = M($table);
        $result = $data->where($where)->delete();
        Addlog();
        return $result;
    }




    /*******************************添加数据**************************************/
    /**
     * 添加数据
     * $table string  表名
     * $param array  数组
     */
    public function add_data($table,$param){
        $data = M($table);
        $result = $data->data($param)->add();
        Addlog();
        return $result;
    }

    /**
     * 添加所有数据
     * $table string  表名
     * $param array  数组
     */
    public function add_all_data($table,$param){
        $data = M($table);
        $result = $data->addall($param);
        Addlog();
        return $result;
    }

    /*********************************************************************/


    /**
     * 获取单表分页数据
     * $data['table'] 数据表
     * $data['field'] 字段		示例:	$data['field']		= array('field1', 'field2', 'field3');
     * $data['where'] 查询条件	示例:	$data['where']		= array('where1'=>'where1', 'where2'=>'where2');
     * $data['limit'] 分页		示例:	$data['limit']		= '1,10';
     * $data['order'] 排序		示例：	$data['order']		= 'id DESC';
     * @return array
     */
    public function fetchPage($table, $fieldArray, $whereArray=array(), $limit, $orderby)
    {
        $record = M($table);
        $field = '*';
        if (!empty($fieldArray))
        {
            $field = implode(', ', $fieldArray);
        }
        $count	= $record->where($whereArray)->count();	//取得符合条件的统计条数
        $result = $record->field($field)	//字段
        ->where($whereArray)		//查询条件
        ->order($orderby)			//排序
        ->page($limit)				//分页限制
        //->cache(true)				//数据缓存
        ->fetchSql(false)			//打印执行的SQL
        ->select();
        $data['count']	= $count;
        $data['list']	= $result;
        return $data;
    }

    /**
     * 获取多表分页数据(最多支持3张表)
     * $data['table'] 数据表		示例：	$data['table']	= array('table1', 'table2', 'table3');	默认实例化table1表，不需要表前缀
     * $data['field'] 字段		示例：	$data['field']	= array('table1.field1', 'table2.field2', 'table3.field3');
     * $data['on']	  连接条件	示例：	$data['on']		= array('table1.field1 = table2.field1', 'table1.field1 = table3.field1');
     * $data['where'] 查询条件	示例：	$data['where']	= array('table1.where1'=>'where1', 'table1.where2'=>'where2');
     * $data['limit'] 分页		示例：	$data['limit']	= '1,10';
     * $data['order'] 排序		示例：	$data['order']	= 'table1.id DESC';
     * @return array
     */
    public function fetchManyPages($table=array(), $fieldArray=array(), $on=array(), $whereArray=array(), $limit, $orderby)
    {
        if (count($table) > 3)
        {
            echo '数据表不能大于3张';
            exit;
        }
        $record	= M($table[0]);
        foreach($table as $key=>$value)
        {
            $table[$key] = C('DB_PREFIX').$value;
        }
        $field = '*';
        if (!empty($fieldArray))
        {
            $field = implode(', ', $fieldArray);
        }
        if (count($table) == 2)
        {
            $count	= $record->join("{$table[1]} ON {$on[0]}", 'LEFT')
                ->where($whereArray)
                ->count();
            $result = $record->field($field)
                ->join("{$table[1]} ON {$on[0]}", 'LEFT')
                ->where($whereArray)
                ->order($orderby)
                ->page($limit)
                //->cache(true)
                ->fetchSql(false)
                ->select();
        }
        elseif (count($table) == 3)
        {
            $count	= $record->join("{$table[1]} ON {$on[0]}", 'LEFT')
                ->join("{$table[2]} ON {$on[1]}", 'LEFT')
                ->where($whereArray)
                ->count();
            $result = $record->field($field)
                ->join("{$table[1]} ON {$on[0]}", 'LEFT')
                ->join("{$table[2]} ON {$on[1]}", 'LEFT')
                ->where($whereArray)
                ->order($orderby)
                ->page($limit)
                //->cache(true)
                ->fetchSql(false)
                ->select();
        }

        $data['count']	= $count ? $count : 0;
        $data['list']	= $result ? $result : null;
        return $data;
    }
}

?>



